import { useReactive } from "ahooks";
import { useModel } from "umi";
import compress from "gql-compress";

import useMember from "../member/useMember";

const useSyncInfoFromMember = () => {
    const { graphqlFetch } = useModel("global");
    const { loadMember } = useMember();

    const state = useReactive({
        loading: false,
    });

    const syncStaffInfoFromMember = async (values, callback) => {
        state.loading = true;
        let member = await loadMember(values.omUuid);
        if (member.omUuid) {
            let res = await graphqlFetch({
                query,
                variables: {
                    osUuid: values.osUuid,
                    osName: member.omName,
                    pyName: pinyin(member.omName.trim(), { toneType: "none", nonZh: "consecutive" }),
                    osRoge: member.omRoge,
                    osPhon: member.omPhon,
                    osNati: member.omNati,
                    osIdnu: member.omIdnu,
                    osBirt: member.omBirt,
                },
            });
            res && callback && callback(res);
        }
        state.loading = false;
    };

    return { ...state, syncStaffInfoFromMember };
};

let query = compress(`mutation MyMutation(
    $osUuid: UUID!
    $osName: String!
    $pyName: String!
    $osRoge: String!
    $osPhon: String
    $osNati: String
    $osIdnu: String
    $osBirt: Date
  ) {
    updateOrganizationStaffByOsUuid(
        input: {
            osUuid: $osUuid
            organizationStaffPatch: {
                osName: $osName
                pyName: $pyName
                osRoge: $osRoge
                osPhon: $osPhon
                osNati: $osNati
                osIdnu: $osIdnu
                osBirt: $osBirt
            }
        }
    ) {
        clientMutationId
    }
}`);

export default useSyncInfoFromMember;
